﻿using System;
using System.Web;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCHeader : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			GetLoginStatus();

			var user = SiteContext.Current.CurrentUser;

			if(user != null)
			{
				lbtnExit.Text = "退出";

				if(user.Role.RoleName.ToLower().Equals("admin"))
				{
					ddlDrugStore.Visible = true;
					imgDrugStorePic.Visible = false;

					if(!IsPostBack)
					{
						BindDrugStore();

						var selectedDrugStoreID = SiteContext.GetCookie(SiteContext.CONST_DRUGSTOREID);

						if(!string.IsNullOrEmpty(selectedDrugStoreID))
						{
							ddlDrugStore.SelectedValue = selectedDrugStoreID;
						}
					}

					if(!string.IsNullOrEmpty(ddlDrugStore.SelectedValue))
					{
						//var selectedDrugStoreID = SiteContext.GetCookie(SiteContext.CONST_DRUGSTOREID);

						//if(!string.IsNullOrEmpty(selectedDrugStoreID))
						//{
						//  ddlDrugStore.SelectedValue = selectedDrugStoreID;
						//}

						if(SiteContext.Current.CurrentDrugStore == null)
						{
							var drugstoreID = int.Parse(ddlDrugStore.SelectedValue);

							var drugStoreProvider = new DrugStoreProvider();

							SiteContext.Current.CurrentDrugStore = drugStoreProvider.GetDrugStore(drugstoreID);
						}
					}
				}
				else
				{
					ddlDrugStore.Visible = false;

					if(user.DrugStore != null && user.DrugStore.DrugStorePic != null)
					{
						imgDrugStorePic.Visible = true;
						imgDrugStorePic.ImageUrl = "~/ImageHandler.ashx?picturesize=100&drugstoreid=" + user.DrugStoreID.ToString();
						imgDrugStorePic.AlternateText = user.DrugStore.DrugStoreName;
						imgDrugStorePic.ToolTip = user.DrugStore.DrugStoreName;
					}
					else
					{
						imgDrugStorePic.Visible = false;
					}
				}
			}
			else
			{
				ddlDrugStore.Visible = false;
				lbtnExit.Text = "登入";
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			//var selectedDrugStoreID = SiteContext.GetCookie(SiteContext.CONST_DRUGSTOREID);

			//if(!string.IsNullOrEmpty(selectedDrugStoreID))
			//{
			//  ddlDrugStore.SelectedValue = selectedDrugStoreID;
			//}
		}

		protected void ddlDrugStore_SelectedIndexChanged(object sender, EventArgs e)
		{
			var selectedDrugstoreID = ddlDrugStore.SelectedValue;

			if(!string.IsNullOrEmpty(selectedDrugstoreID))
			{
				SiteContext.SetCookie(HttpContext.Current.ApplicationInstance, SiteContext.CONST_DRUGSTOREID, selectedDrugstoreID);
				//ddlDrugStore.SelectedValue = selectedDrugstoreID;

				var drugstoreID = int.Parse(selectedDrugstoreID);

				var drugStoreProvider = new DrugStoreProvider();

				SiteContext.Current.CurrentDrugStore = drugStoreProvider.GetDrugStore(drugstoreID);
			}
		}

		private void BindDrugStore()
		{
			var drugStoreProvider = new DBProvider.Providers.DrugStoreProvider();

			ddlDrugStore.DataSource = drugStoreProvider.GetAllDrugStore();
			ddlDrugStore.DataBind();

			if(ddlDrugStore.Items != null && ddlDrugStore.Items.Count > 0)
			{
				ddlDrugStore.SelectedIndex = 0;
			}
		}

		private void GetLoginStatus()
		{
			var user = SiteContext.Current.CurrentUser;

			if(user != null)
			{
				ltrLoginStatus.Text = user.UserCName + " 欢迎您";

				if(user.Picture == null)
				{
					imgHeader.ImageUrl = "~/template/noimage.jpg";
				}
				else
				{
					imgHeader.ImageUrl = "~/ImageHandler.ashx?picturesize=34&userid=" + user.UserID.ToString();
				}
			}
			else
			{
				imgHeader.Visible = false;
				ltrLoginStatus.Text = string.Empty;
			}
		}

		protected void lbtnExit_Click(object sender, EventArgs e)
		{
			ltrLoginStatus.Text = string.Empty;
			SiteContext.SetCookie(HttpContext.Current.ApplicationInstance, SiteContext.CONST_USERSESSIONCOOKIE, string.Empty);
			SiteContext.Current.CurrentUser = null;
			Response.Redirect("Login.aspx");
		}
	}
}